<template>
  <div>
    <div class='shopcart'>
      <div class="left"
           @click='toggleList'>
        <div class='icon'
             :class="{'highlight':totalCount>0}">
          <i class="icon-shopping_cart"
             :class="{'highlight':totalCount>0}"></i>
          <div class='num'
               v-show='totalCount>0'>{{totalCount}}</div>
        </div>
        <span class='price'
              :class="{'highlight':totalPrice>0}">￥{{totalPrice}}</span>
        <span class='delivery'>另需配送费{{deliveryPrice}}元</span>
      </div>
      <div class="
           right"
           @click.stop.prevent='pay'>
        <div class="pay"
             :class="payClass">{{payDesc}}</div>
      </div>
      <div class='ball-container'>

        <div v-for='(ball,index) in balls'
             :key='index'>
          <transition name='drop'
                      @before-enter='beforeEnter'
                      @enter='enter'
                      @after-enter='afterEnter'>
            <div class="ball"
                 v-show='ball.show'>
              <div class="inner inner-hook">1</div>
            </div>
          </transition>
        </div>

      </div>
      <transition name='fold'>
        <div class='shoplist'
             v-show='listshow'>
          <div class="listheader">
            <h1 class='title'>购物车</h1>
            <span class='empty'
                  @click='empty'>清空</span>
          </div>
          <div class="listcontent"
               ref='listcontent'>
            <ul>
              <li v-for='(food,index) in selectFoods'
                  :key='index'
                  class='item'>
                <span class='name'>{{food.name}}</span>
                <span class='price'>￥{{food.price*food.count}}</span>
                <div class='cartcontrol'>
                  <cartcontrol :food='food'></cartcontrol>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </transition>

    </div>
    <transition name='mask'>
      <div class="listmask"
           v-show='listshow'></div>
    </transition>
  </div>
</template>
<script>
import cartcontrol from '../cartcontrol/cartcontrol'
import BScroll from 'better-scroll'
export default {
  name: 'shopcart',
  components: {
    cartcontrol
  },
  data () {
    return {
      balls: [{
        show: false
      },
      {
        show: false
      },
      {
        show: false
      },
      {
        show: false
      },
      {
        show: false
      }],
      dropballs: [],
      listshow: false
    }
  },
  methods: {
    empty () {
      this.selectFoods.forEach((food) => {
        food.count = 0
      })
      this.listshow = false
    },
    toggleList () {
      if (!this.totalCount) {
        return
      }
      this.listshow = !this.listshow
    },
    beforeEnter (el) {
      let count = this.balls.length
      while (count--) {
        let ball = this.balls[count]
        if (ball.show) {
          let rect = ball.el.getBoundingClientRect()
          let x = rect.left - 32
          let y = -(window.innerHeight - rect.top - 30)
          el.style.display = ''
          el.style.webkitTransform = `translate3d(0,${y}px,0)`
          el.style.transform = `translate3d(0,${y}px,0)`
          let inner = el.getElementsByClassName('inner-hook')[0]
          inner.style.webkitTransform = `translate3d(${x}px,0,0)`
          inner.style.transform = `translate3d(${x}px,0,0)`
        }
      }
    },
    enter (el) { // 浏览器重绘
      /* eslint-disable no-unused-vars */
      let rf = el.offsetHeight
      this.$nextTick(() => {
        el.style.webkitTransform = 'translate3d(0,0,0)'
        el.style.transform = 'translate3d(0,0,0)'
        let inner = el.getElementsByClassName('inner-hook')[0]
        inner.style.webkitTransform = 'translate3d(0,0,0)'
        inner.style.transform = 'translate3d(0,0,0)'
      })
    },
    afterEnter (el) {
      let ball = this.dropballs.shift()
      if (ball.show) {
        ball.show = false
        el.style.display = 'none'
      }
    },
    drop (el) {
      for (let i = 0; i < this.balls.length; i++) {
        let ball = this.balls[i]
        if (!ball.show) {
          ball.show = true
          ball.el = el
          this.dropballs.push(ball)
          return
        }
      }
    },
    pay () {
      if (this.totalPrice < this.minPrice) {
        return
      }
      window.alert('支付')
    }
  },
  props: {
    selectFoods: {
      type: Array,
      default () {
        return []
      }
    },
    deliveryPrice: {
      type: Number,
      default: 0
    },
    minPrice: {
      type: Number,
      default: 20
    }
  },
  computed: {
    totalPrice () {
      let total = 0
      this.selectFoods.forEach((food) => {
        total += food.price * food.count
      })
      return total
    },
    totalCount () {
      let count = 0
      this.selectFoods.forEach((food) => {
        count += food.count
      })
      return count
    },
    payDesc () {
      if (this.totalPrice === 0) {
        return `￥${this.minPrice}元起送`
      } else if (this.totalPrice < this.minPrice) {
        return `还差￥${this.minPrice - this.totalPrice}起送`
      } else {
        return '去结算'
      }
    },
    payClass () {
      if (this.totalPrice < this.minPrice) {
        return 'not-enough'
      } else {
        return 'enough'
      }
    }
  },
  watch: {
    totalCount: function () {
      if (!this.totalCount) {
        this.listshow = false
      }
    },
    listshow: function () {
      if (!this.totalCount) {
        this.empty()
      }
      let show = this.listshow
      if (show) {
        this.$nextTick(() => {
          if (!this.scroll) {
            this.scroll = new BScroll(this.$refs.listcontent, {
              click: true
            })
          } else {
            this.scroll.refresh()
          }
        })
      }
      return show
    }
  }

}

</script>
<style lang="stylus" scoped>
@import '../../common/stylus/icon.styl'
.listmask
  position fixed
  top 0
  left 0
  width 100%
  height 100%
  z-index 50
  background rgba(7, 17, 27, 0.8)
  backdrop-filter blur(10px)
  &.mask-enter-active, &.mask-leave-active
    transition all 0.4s
.shopcart
  height 58px
  width 100%
  background-color #141d27
  position absolute
  bottom 0
  display flex
  z-index 999
  .ball-container
    .ball
      position fixed
      left 32px
      bottom 22px
      transition all 0.4s cubic-bezier(0.49, -0.29, 0.75, 0.41)
      .inner
        width 16px
        height 16px
        background red
        border-radius 50%
        transition all 0.4s
        color #fff
        text-align center
  .shoplist
    position absolute
    z-index 999
    left 0
    bottom 58px
    width 100%
    &.fold-enter-active, &.fold-leave-active
      transition all 0.4s linear
    &.fold-enter, &.fold-leave-to
      opacity 0
      transform translate3d(0, 0, 0)
    .listcontent
      max-height 217px
      background #f3f5f7
      overflow hidden
      .item
        height 50px
        line-height 50px
        .name, .price
          font-size 14px
          color rgb(7, 17, 27)
          padding 0 18px
        .price
          display inline-block
          position absolute
          left 150px
        .cartcontrol
          float right
    .listheader
      height 40px
      line-height 40px
      padding 0 18px
      background #f3f5f7
      border-bottom 1px solid rgba(7, 17, 27, 0.1)
      .title
        float left
        font-size 14px
        color rgb(7, 17, 27)
      .empty
        float right
        font-size 12px
        color rgb(0, 160, 220)
  .left
    height 100%
    flex 1
    position relative
    .icon
      width 58px
      height 58px
      line-height 58px
      border-radius 50%
      background #141d27
      text-align center
      position absolute
      left 20px
      top -12px
      z-index 9999
      &.highlight
        background rgb(0, 160, 220)
      .num
        position absolute
        top 0
        right 0
        width 24px
        height 16px
        line-height 16px
        text-align center
        font-size 9px
        font-weight 700
        color #fff
        background red
        border-radius 16px
        box-shadow 0 4px 8px 0 rgba(0, 0, 0, 0.4)
      .icon-shopping_cart
        font-size 24px
        color rgba(255, 255, 255, 0.4)
        &.highlight
          color #fff
    .price
      color rgba(255, 255, 255, 0.4)
      font-size 12px
      position absolute
      left 80px
      top 0
      padding 23px 23px
      &.highlight
        color #fff
    .delivery
      color rgba(255, 255, 255, 0.4)
      font-size 12px
      position absolute
      right 20px
      top 0
      padding 23px 23px
  .right
    height 100%
    background #2b333b
    flex 0 0 105px
    width 105px
    display table
    .pay
      display table-cell
      vertical-align middle
      font-weight 700
      font-size 12px
      line-height 24px
      text-align center
      color rgba(255, 255, 255, 0.4)
      &.not-enough
        background #2b333b
      &.enough
        background #00b43c
        color #fff
</style>
